home *** CD-ROM | disk | FTP | other *** search
- Path: radius.mae.cornell.edu!schmidt
- From: schmidt@radius.mae.cornell.edu (Peter Schmidt)
- Newsgroups: comp.lang.c
- Subject: reattaching stdin to fd 0
- Date: 7 Mar 1996 17:49:14 GMT
- Organization: CU-HSS Program in Biomechanical Engineering
- Sender: ps17@cornell.edu (Verified)
- Message-ID: <4hn7iq$83b@newsstand.cit.cornell.edu>
- NNTP-Posting-Host: radius.mae.cornell.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- I have a problem which I find is very poorly documented.
-
- My program attaches stdin to a file, while saving the original stdin, like
- this:
-
- char *fn;
- int fd;
-
- if ((fd = open(fn, O_RDONLY)) < 0){
- perror(fn);
- exit(1);
- }
- sin2 = dup(0);
- close(0);
- dup2(fd, 0);
-
- then, when it is done reading from the file, I re-attach the original
- stdin, like this:
-
- close(0);
- close(fd);
- dup2(sin2, 0);
- close(sin2);
-
- After doing this, I can read from the keyboard with
-
- read(0, linebuf, sizeof(linebuf));
-
- but if I use any of the stdlib routines, the program reads garbage. It
- seems that after re-attaching stdin to fd 0, stdin and fd 0 do not point to
- the same stream.
-
- Any help on getting stdin pointing back to the keyboard would be apreciated.
-
- Please e-mail replies to ps17@cornell.edu.
-
- Thanks in advance
-
- Peter Schmidt
- ps17@cornell.edu
-